package org.yxing.palpalgps.widget;

import org.yxing.palpalgps.image.ImageCache;
import org.yxing.palpalgps.image.ImageLoader;
import org.yxing.palpalgps.image.ImageLoader.ImageLoaderCallback;
import org.yxing.palpalgps.image.ImageRequest;
import org.yxing.palpalgps.utils.Helper;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

public class AsyncImageView extends ImageView implements ImageLoaderCallback {
	
	private static final String TAG = AsyncImageView.class.getSimpleName();
	
	private static final int IMAGE_SOURCE_UNKNOWN = -1;
	private static final int IMAGE_SOURCE_BITMAP = 1;
	private static final int IMAGE_SOURCE_RESOURCE = 2;
	private static final int IMAGE_SOURCE_DRAWABLE = 3;
	
	private Bitmap mDefaultBitmp;
	private int mDefaultResoutce;
	private Drawable mDefaultDrawable;
	
	private String mUrl;
	private ImageRequest mImageRequest;
	
	private int mImageSource;
	
	public AsyncImageView(Context context) {
		this(context, null);
	}
	
	public AsyncImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
	
	public AsyncImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		
		mImageSource = IMAGE_SOURCE_UNKNOWN;
	}
	
	public void setUrl(String url) {
		mUrl = url;
		if (mImageRequest != null) {
			mImageRequest.cancel();
			mImageRequest = null;
		}
		
		ImageCache imagecache = Helper.getApplication(getContext()).getImageCache();
		Bitmap bitmap = imagecache.get(url);
		if (bitmap == null) {
			setDefaultImage();
			mImageRequest = new ImageRequest(getContext(), mUrl, this);
			mImageRequest.load();
		} else {
			setImageBitmap(bitmap);
		}
		
	}
	
	public void setImageBitmap() {
		ImageCache imagecache = Helper.getApplication(getContext()).getImageCache();
		Bitmap bitmap = imagecache.get(mUrl);
		if (bitmap != null) {
			setImageBitmap(bitmap);
		} else 
			setDefaultImage();
	}
	
//	public Bitmap resize(Bitmap bitmap) {
//		if (newWidth == 0) {
//			return bitmap;
//		}
//		
//		int width = bitmap.getWidth();
//		int height = bitmap.getHeight();
//		float scale = newWidth/width;
//		
//		Matrix mMatrix = new Matrix();
//		mMatrix.postScale(scale, scale);
//		return Bitmap.createBitmap(bitmap, 0, 0, width, height, mMatrix, true);
//	}
	
	public void setDefaultBitmap(Bitmap bitmap) {
		mImageSource = IMAGE_SOURCE_BITMAP;
		mDefaultBitmp = bitmap;
		setImageBitmap(bitmap);
	}
	
	public void setDefaultResource(int resource) {
		mImageSource = IMAGE_SOURCE_RESOURCE;
		mDefaultResoutce = resource;
		setImageResource(resource);
	}
	
	public void setDefaultDrawable(Drawable drawable) {
		mImageSource = IMAGE_SOURCE_DRAWABLE;
		mDefaultDrawable = drawable;
		setImageDrawable(drawable);
	}
	
	public void setDefaultImage() {
		switch (mImageSource) {
		case IMAGE_SOURCE_BITMAP:
			setImageBitmap(mDefaultBitmp);
			break;
		case IMAGE_SOURCE_DRAWABLE:
			setImageDrawable(mDefaultDrawable);
			break;
		case IMAGE_SOURCE_RESOURCE:
			setImageResource(mDefaultResoutce);
			break;
		default:
			setImageDrawable(null);
		}
	}
	

	@Override
	public void onImageLoderStarted(ImageLoader loader) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onImageLoderEnded(ImageLoader loader, Bitmap bitmap) {
		// TODO Auto-generated method stub
		setImageBitmap();
	}

	@Override
	public void onImageLoaderFall(ImageLoader loader, Throwable exception) {
		// TODO Auto-generated method stub
		
	}
}
